<template>
    <el-table row-key="id" default-expand-all border :data="PermissionArr">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="权限值" prop="code" />
        <el-table-column label="修改时间" prop="updateTime" />
        <el-table-column label="操作">
            <template #="{ row, $index }">
                <el-button size="small" type="primary" @click="addPermission(row)"
                    :disabled="row.level == 4 ? true : false">{{ row.level
                        == 3 ? "添加功能" : "添加菜单" }}</el-button>
                <el-button size="small" type="primary" @click="updataPermission(row)"
                    :disabled="row.level == 1 ? true : false">编辑</el-button>
                <el-popconfirm :title="`你确定要删除${row.name}吗？`" style="width: 260px;" @confirm="remove(row.id)">
                    <template #reference>
                        <el-button size="small" type="primary" @click=""
                            :disabled="row.level == 1 ? true : false">删除</el-button>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '新增菜单'">
        <el-form>
            <el-form-item label="名称" prop="">
                <el-input placeholder="请输入菜单名称" v-model="menuData.name" </el-input>
            </el-form-item>
            <el-form-item label="权限" prop="">
                <el-input placeholder="请输入权限值" v-model="menuData.code"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="save">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang='ts' name="Permission">
import { ref, onMounted, reactive } from 'vue';
import { reqGetPermission, reqAddPermissionOrUpdate, reqRemovePermission } from '@/api/acl/menu/index'
import type { PermissionResponseData, PermissionList, Permission, MenuParams } from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus';
let PermissionArr = ref<PermissionList>([])
let dialogVisible = ref<boolean>(false)
let menuData = reactive<MenuParams>({
    code: '',
    level: 0,
    name: '',
    pid: 0,
})
onMounted(() => {
    getHasPermission()
})
const getHasPermission = async () => {
    let result: PermissionResponseData = await reqGetPermission()
    if (result.code == 200) {
        PermissionArr.value = result.data
    }
}
const addPermission = (row: Permission) => {
    Object.assign(menuData, {
        id: 0,
        code: '',
        level: 0,
        name: '',
        pid: 0,
    })
    dialogVisible.value = true
    menuData.level = row.level + 1
    menuData.pid = (row.id as number)
}
const updataPermission = (row: Permission) => {
    dialogVisible.value = true
    Object.assign(menuData, row)
}
const save = async () => {
    let result: any = await reqAddPermissionOrUpdate(menuData)
    console.log(result);

    if (result.code == 200) {
        dialogVisible.value = false
        ElMessage({ type: 'success', message: menuData.id ? '更新成功' : '新增成功' })
        getHasPermission()
    }
}
const remove = async (id: number) => {
    let result: any = await reqRemovePermission(id)
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '删除成功' })
        getHasPermission()
    }
}
</script>

<style></style>